// store/modules/history.js

const state = {
  interviewHistories: [] // 保存所有面试历史记录
}

const mutations = {
  // 添加新的面试历史记录
  ADD_INTERVIEW_HISTORY(state, history) {
    state.interviewHistories.push({
      id: Date.now(), // 使用时间戳作为唯一ID
      date: new Date().toLocaleString(),
      records: [...history] // 深拷贝历史记录
    })
  },

  // 清空所有历史记录
  CLEAR_INTERVIEW_HISTORIES(state) {
    state.interviewHistories = []
  }
}

const actions = {
  // 保存面试历史记录
  saveInterviewHistory({ commit }, history) {
    commit('ADD_INTERVIEW_HISTORY', history)
  },

  // 清空历史记录
  clearInterviewHistories({ commit }) {
    commit('CLEAR_INTERVIEW_HISTORIES')
  }
}

const getters = {
  // 获取所有面试历史
  getAllInterviewHistories: state => state.interviewHistories,

  // 获取最近的面试历史
  getLatestInterviewHistory: state => {
    return state.interviewHistories.length > 0
      ? state.interviewHistories[state.interviewHistories.length - 1]
      : null
  },

  // 按日期筛选面试历史
  getInterviewHistoriesByDate: state => date => {
    return state.interviewHistories.filter(history =>
      new Date(history.date).toDateString() === new Date(date).toDateString()
    )
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
